package com.ruihui.activity.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruihui.activity.exceprion.ManualException;
import com.ruihui.activity.mapper.AcgoodsMapper;
import com.ruihui.activity.mapper.AcgrecordMapper;
import com.ruihui.activity.pojo.Acgoods;
import com.ruihui.activity.pojo.Acgrecord;
import com.ruihui.activity.service.AcgoodsService;
import com.ruihui.activity.util.ConstantHolder;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cf123
 * @since 2018-09-30
 */
@Service
public class AcgoodsServiceImpl extends ServiceImpl<AcgoodsMapper, Acgoods> implements AcgoodsService {
	@Autowired
	private AcgoodsMapper acgoodsMapper;
	@Autowired
	private AcgrecordMapper acgrecordMapper;
	@Autowired
	private StringRedisTemplate redisTemplate;
	public PageInfo<Acgoods> getACgoodsList(Map map) {
		int pageNo = 0;
		int pageSize = 0;

		if (!StringUtils.isEmpty(map.get("pageNo"))) {
			pageNo = Integer.parseInt(map.get("pageNo").toString());
		}
		if (!StringUtils.isEmpty(map.get("pageSize"))) {
			pageSize = Integer.parseInt(map.get("pageSize").toString());
		}
		if (pageNo > 0 && pageSize > 0) {
			PageHelper.startPage(pageNo, pageSize);
		}
		PageHelper.orderBy("sorting");
		List<Acgoods> list = acgoodsMapper.getACgoodsList(map);
		return new PageInfo<Acgoods>(list);
	}

	@Transactional
	public int acreduceNum(Acgoods acgoods, Integer buyNum, Long id, Long nowdate, Long memberId) {
		int acnum = acgoods.getAcnum() - buyNum;
		int status = 0,renum=1;
		acgoods.setAcnum(acnum);
		while (renum<=3&&status!=1) {
			status = acgoodsMapper.update(acgoods,
				new EntityWrapper<Acgoods>().eq("id", acgoods.getId()));
			renum++;
		}
		if (status > 0) {
			// 新增记录
			Acgrecord acgrecord = new Acgrecord();
			acgrecord.setAcid(acgoods.getId());
			acgrecord.setCreatetime(new BigDecimal(nowdate));
			acgrecord.setGoodsid(acgoods.getGoodsid());
			acgrecord.setUserid(memberId);
			acgrecord.setAcnum(buyNum);
			acgrecord.setState(1);
			int reCode = acgrecordMapper.insert(acgrecord);
			if (reCode <= 0) {
				throw new ManualException(-1, "新增记录失败");
			}
		} else {
			throw new ManualException(-1, "更新数据失败");
		}
		/*
		 * 更新缓存
		 */
		Map map = new HashMap();
		map.put("acid", acgoods.getAcid());
		List<Acgoods> acgoodsList = this.getACgoodsList(map).getList();
		String key = String.format(ConstantHolder.ACGLIST, acgoods.getAcid());
		String value = JSON.toJSON(acgoodsList).toString();
		redisTemplate.opsForValue().set(key, value);
		return status;
	}

	public Acgoods iniacGoods( Long goodsid, long nowdate,String types) {

		List<Acgoods> list = acgoodsMapper.selectList(new EntityWrapper<Acgoods>().eq("goodsid", goodsid)
				.eq("state", 1).in("type", types).le("starttime", nowdate).gt("endtime", nowdate));

		if (!list.isEmpty() && list.size() == 1) {
			return list.get(0);
		}
		return null;
	}
	@Transactional
	public Acgoods acBackNum(Long userid, Long acid, Integer buyNum) {
		int acflag = 0,renum=1;
		Acgoods ag = acgoodsMapper.selectById(acid);
		// 活动商品id,查询记录删除其中一条数据
		List<Acgrecord> records = acgrecordMapper
				.selectList(new EntityWrapper<Acgrecord>().eq("acid", acid).eq("userid", userid));
		if (records != null && records.size() > 0) {
			Acgrecord acg = records.get(records.size() - 1);
			//删除记录
			Integer acgflag = acgoodsMapper.deleteById(acg.getId());
			if(acgflag.intValue()<=0){
				throw new ManualException(-1, "恢复库存失败");
			}
			//恢复库存
			if(ag!=null){
				int acnum = ag.getAcnum().intValue() + buyNum.intValue();
				ag.setAcnum(acnum);
				while (renum<=3&&acflag!=1) {
					acflag = acgoodsMapper.updateById(ag);
					renum++;
				}
				ag = acgoodsMapper.selectById(acid);
				if(acflag<=0){
					throw new ManualException(-1, "恢复库存失败");
				}
			}
		}
		return ag;
	}

}
